package com.weibo.dao.impl;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.weibo.dao.IUserDao;
import com.weibo.dao.IWeiboDao;
import com.weibo.pojo.Tcomment;
import com.weibo.pojo.Tweibo;

public class WeiboDaoImpl extends HibernateDaoSupport implements IWeiboDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Tweibo> getHomeWeiboList(Integer userId) throws Exception {
		HibernateTemplate htp = this.getHibernateTemplate();
		
		String hql = "select distinct tweibo from Rfollow rfollow,Tweibo tweibo where rfollow.folUser = ? and tweibo.weiboOwner = rfollow.folToUser" +
				" or tweibo.weiboOwner = ? order by tweibo.weiboPubdate desc";
		List<Tweibo> weiboList = htp.find(hql,new Object[]{userId,userId});
		return weiboList;
	}
	
	@Override
	public void saveWeibo(Tweibo weibo) throws Exception {
		this.getHibernateTemplate().save(weibo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tcomment> getCommentListByWeibo(Integer weiboId)
			throws Exception {
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = "from Tcomment where comWeibo = ?";
		List<Tcomment> commentList = htp.find(hql, weiboId);
		return commentList;
	}

	@Override
	public Tweibo getWeiboById(Integer weiboId) {
		return this.getHibernateTemplate().get(Tweibo.class, weiboId);
	}

	@Override
	public void deleteWeibo(Tweibo weibo) {
		this.getHibernateTemplate().delete(weibo);
	}

}
